import sys
import os
import subprocess
from PyQt5 import QtWidgets, QtCore
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap, QFont

class LaunchWidget(QtWidgets.QWidget):

    WINDOW_WIDTH = 1500
    WINDOW_HEIGHT = 900

    # 初始化
    def __init__(self) -> None:
        super().__init__()
        self.resize(LaunchWidget.WINDOW_WIDTH, LaunchWidget.WINDOW_HEIGHT)
        self.setWindowTitle("My MineCraft Launcher!")
        
        # 获取当前所有世界名称
        worldfiles = os.listdir('./resources/worlds')
        self.worldNams = []
        for worldfile in worldfiles:
            self.worldNams.append(worldfile.split('.')[0])

        # 背景
        self.setBackground('./resources/launcher_back.jpg')

        # 透明背景
        self.widget = QtWidgets.QWidget(self)
        self.widget.move(0, 0)
        self.widget.resize(450, LaunchWidget.WINDOW_HEIGHT)
        with open('./resources/qss/widget.qss', 'r', encoding='UTF-8') as qssfile:
            self.widget.setStyleSheet(qssfile.read())
        op = QtWidgets.QGraphicsOpacityEffect()
        op.setOpacity(0.5)
        self.widget.setGraphicsEffect(op)

        # 启动按钮
        self.startButton = QtWidgets.QPushButton(self)
        self.startButton.move(1150, 730)
        self.startButton.resize(300, 120)
        self.startButton.setText('Start')
        with open('./resources/qss/button.qss', 'r', encoding='UTF-8') as qssfile:
            self.startButton.setStyleSheet(qssfile.read())
        self.startButton.clicked.connect(self.launchGame)

        self.worldLine = QtWidgets.QLineEdit(self)
        self.worldLine.setText('World: ')
        self.worldLine.move(1150, 670)
        self.worldLine.resize(300, 40)
        with open('./resources/qss/lineeditwithe.qss', 'r', encoding='UTF-8') as qssfile:
            self.worldLine.setStyleSheet(qssfile.read())
        self.worldLine.setEnabled(False)
    
        self.gameLine = QtWidgets.QLineEdit(self)
        self.gameLine.setText('Game')
        self.gameLine.move(60, 60)
        self.gameLine.resize(300, 40)
        with open('./resources/qss/lineedit.qss', 'r', encoding='UTF-8') as qssfile:
            self.gameLine.setStyleSheet(qssfile.read())
        self.gameLine.setEnabled(False)

        # 选择世界
        self.pixLabel1 = QtWidgets.QLabel(self)
        self.pixLabel1.move(60, 240)
        pix = QPixmap('./resources/icons/bookshelf.png')
        self.pixLabel1.setPixmap(pix)
        self.selectBtn = QtWidgets.QPushButton(self)
        self.selectBtn.setText('Select World')
        with open('./resources/qss/function_button.qss', 'r', encoding='UTF-8') as qssfile:
            self.selectBtn.setStyleSheet(qssfile.read())
        self.selectBtn.resize(250, 60)
        self.selectBtn.move(130, 240)
        self.selectBtn.clicked.connect(self.selectWorld)

        # 创建世界
        self.pixLabel2 = QtWidgets.QLabel(self)
        self.pixLabel2.move(60, 140)
        pix = QPixmap('./resources/icons/oak_log.png')
        self.pixLabel2.setPixmap(pix)
        self.creatBtn = QtWidgets.QPushButton(self)
        self.creatBtn.setText('Create World')
        with open('./resources/qss/function_button.qss', 'r', encoding='UTF-8') as qssfile:
            self.creatBtn.setStyleSheet(qssfile.read())
        self.creatBtn.resize(250, 60)
        self.creatBtn.move(130, 140)
        self.creatBtn.clicked.connect(self.createWorld)

        self.setLine = QtWidgets.QLineEdit(self)
        self.setLine.setText('Set')
        self.setLine.move(60, 350)
        self.setLine.resize(300, 40)
        with open('./resources/qss/lineedit.qss', 'r', encoding='UTF-8') as qssfile:
            self.setLine.setStyleSheet(qssfile.read())
        self.setLine.setEnabled(False)

        # 选择的世界
        self.currentWord : str = ''

    # 启动游戏
    def launchGame(self):
        subprocess.call(['./bin/exe/minecraft_debug.exe', self.currentWord])

    # 选择世界
    def selectWorld(self):
        worldName, filetype = QtWidgets.QFileDialog.getOpenFileName(self, filter='World Files(*.wd)')
        worldName = (worldName.split('/')[-1]).split('.')[0]
        self.setSelectWorld(worldName)

    # 创造世界
    def createWorld(self):
        inputDialog = QtWidgets.QInputDialog(self)
        worldName, ok = inputDialog.getText(self, 'create world', 'input new world name')
        if ok:
            if worldName in self.worldNams:
                QtWidgets.QMessageBox.critical(self, 'create error!', '{} already exit!'.format(worldName))
                return
            print(worldName)
            self.setSelectWorld(worldName)
            self.worldNams.append(worldName)
            self.launchGame()

    # 设置窗口背景
    def setBackground(self, imag_path):
        palette = QtGui.QPalette()
        palette.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap(imag_path)))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

    # 设置选中世界
    def setSelectWorld(self, worldName):
        self.currentWord = './resources/worlds/{}.wd'.format(worldName)
        self.worldLine.setEnabled(True)
        self.worldLine.setText('World: {}'.format(worldName))
        self.worldLine.setEnabled(False)

# 主函数
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    widget = LaunchWidget()
    widget.show()
    sys.exit(app.exec_())